import { defineConfig, UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import path from 'path'
import viteCompression from 'vite-plugin-compression'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// import dynamicImportVars from '@rollup/plugin-dynamic-import-vars'
// https://vitejs.dev/config/
export default defineConfig(({ mode }: UserConfig): UserConfig => {
  return {
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      vueJsx({
        // options are passed on to @vue/babel-plugin-jsx
      }),

      viteCompression({
        filter: /\.(js|mjs|json|css|html)$/i, // 匹配文件名
        threshold: 10240, // 对超过10k的数据压缩
        deleteOriginFile: false, // 不删除源文件
      }),
    ],
    build: {
      minify: 'terser', // 移除console 必须要配置
      terserOptions: {
        compress: {
          drop_console: true, // 移除console
          drop_debugger: true, // 移除debugger
        },
      },
    },
    css: {
      //css预处理
      preprocessorOptions: {
        scss: {
          //引入var.scss全局预定义变量
          additionalData: '@import "@/assets/scss/globals.scss";',
        },
      },
    },
    server: {
      host: '0.0.0.0',
      // proxy: {
      //   '/api': {
      //     target: 'http://localhost:3111',
      //     // target: 'http://146.56.207.205:3111',
      //     changeOrigin: true,
      //     rewrite: (path) => path.replace(/^\/api/, ''),
      //   },
      // },
      hmr: true,
    },
    resolve: {
      alias: {
        // 配置项目的文件的绝对定位 ----- 需要在tsconfig.json文件中配置 防止ts报错
        '@': path.resolve(__dirname, 'src'),
        // '@components': path.resolve(__dirname, 'src/components'),
        // '@api': path.resolve(__dirname, 'src/api'),
        // '@view': path.resolve(__dirname, 'src/view'),
        // '@router': path.resolve(__dirname, 'src/router'),
        // '@utils': path.resolve(__dirname, 'src/utils'),
        // '@modules': path.resolve(__dirname, 'src/modules'),
        // '@config': path.resolve(__dirname, 'src/config'),
        // '@store': path.resolve(__dirname, 'src/store'),
        // '@assets': path.resolve(__dirname, 'src/assets'),
      },
    },
  }
})
